<?php

namespace mpend\modules\activity\controllers;

use Yii;
use mpend\components\Controller;
use mpend\modules\activity\models\ActivityView;
use mpend\modules\activity\models\ActivityEnrollForm;

/**
 * 活动报名
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class EnrollController extends Controller {

    /**
     * @inheritdoc
     */
    protected function verbs() {
        return [
            '*' => ['GET'],
            'update' => ['POST'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actionIndex($id) {
        $activity = ActivityView::findOne($id);
        if ($activity->canEnroll) {
            return $activity->form;
        }
        return parent::error('报名活动暂未开启');
    }

    /**
     * 提交认证
     * @inheritdoc
     */
    public function actionUpdate($id) {
        $model = new ActivityEnrollForm();
        $model->activity_id = $id;
        $model->user_id = $this->uid;
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请完善信息后提交');
        }
        return $model->save();
    }

}
